<?
session_start();
if ((!isset($_SESSION["sess_Username"])) and (!isset($_SESSION["sess_user_register"]))) { 
?>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<script>
alert('กรุณาทำการลงทะเบียนหรือ login เข้าสู่ระบบก่อน');
function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
</script>
	<meta http-equiv='refresh' content='0;URL=user_register.php'>
<?

 }
  else if ((isset($_SESSION["sess_user_register"])) or (isset($_SESSION["sess_Username"])))
 {
include "connectdb.php" ;
 ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>ยินดีต้อนรับเข้าสู่ ระบบบริการสื่อมัลติมีเดียด้านการเกษตร</title>
<link rel="stylesheet" href="standard.css" type="text/css" />
<script>  
	function txtreset()
		{
		document.frmSearch.txtkeyword.value="";
		}
</script>
<?php 
function remember(){

		for ($i=0;$i<count($_GET["cb_array"]);$i++) {
				$cb_array = $cb_array[$i];
			}
			
}
?>


<script type="text/javascript">
<!--
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

//-->
</script>

</head>

<body onLoad="MM_preloadImages('img/m_main2.jpg','img/m_register2.jpg','img/m_multimedia2.jpg','img/m_advancesearch2.jpg','img/m_manual2.jpg','img/m_evaluate2.jpg','img/m_admin2.jpg','img/m_static2.jpg')">
<table width="1024" height="659" border="0" align="center" cellpadding="0" cellspacing="0" bordercolor="#666600">
  <tr>
    <td height="215" align="center" valign="top" bgcolor="#CCCCCC"><table width="1024" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td height="170" background="img/headNew2.jpg" bgcolor="#FFFFFF">&nbsp;</td>
      </tr>
    </table>
        <table width="824" border="0" cellpadding="0" cellspacing="0" bordercolor="#FF0000">
          <tr>
            <td width="103" height="45" align="center"><a href="index.php" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('Image12','','img/m_main2.jpg',1)"><img src="img/m_main.jpg" name="Image12" width="103" height="45" border="0" id="Image12" /></a></td>
            <td width="103" align="right"><a href="user_register.php" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('Image13','','img/m_register2.jpg',1)"><img src="img/m_register.jpg" name="Image13" width="103" height="45" border="0" id="Image13" /></a></td>
            <td width="103" align="right"><a href="vdo_list.php" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('Image14','','img/m_multimedia2.jpg',1)"><img 
src="img/m_multimedia.jpg" name="Image14" width="103" height="45" border="0" id="Image14" /></a></td>
            <td width="103"><a href="vdo_advance_search.php" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('Image11','','img/m_advancesearch2.jpg',1)"><img 
src="img/m_advancesearch.jpg" name="Image11" width="206" height="45" border="0" id="Image11" /></a></td>
            <td width="103"><a href="admin_report_search.php" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('Image10','','img/m_static2.jpg',1)"><img 
src="img/m_static.jpg" name="Image10" width="200" height="45" border="0" id="Image10" /></a></td>
            <td width="103"><a href="manual.php" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('Image9','','img/m_manual2.jpg',1)"><img src="img/m_manual.jpg" name="Image9" width="103" height="45" border="0" id="Image9" /></a></td>
            <td width="103"><a href="evaluate_overview.php" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('Image8','','img/m_evaluate2.jpg',1)"><img src="img/m_evaluate.jpg" name="Image8" width="103" height="45" border="0" id="Image8" /></a></td>
            <td width="103"><a href="admin_login.php" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('Image15','','img/m_admin2.jpg',1)"><img src="img/m_admin.jpg" name="Image15" width="103" height="45" border="0" id="Image15" /></a></td>
          </tr>
        </table></td>
  </tr>
  <tr>
    <td width="1024" height="66" align="center" valign="top" bgcolor="#FFFFFF"><table width="1024" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td width="25" height="25" background="img/tb/tb_left_top.jpg">&nbsp;</td>
        <td height="25" background="img/tb/tb_top.jpg">&nbsp;</td>
        <td width="25" height="25" background="img/tb/tb_right_top.jpg">&nbsp;</td>
      </tr>
      <tr>
        <td width="25" align="left" background="img/tb/tb_left.jpg">&nbsp;</td>
        <td align="center"><table width="100%" border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td height="30" align="left" bgcolor="#CCFF00" class="style7"><span class="scapmain style7">&nbsp;<img src="img/img05.gif" align="absbottom" /></span> สืบค้นโดยใช้แนวคิดออนโทโลยี</td>
            <td align="right" bgcolor="#CCFF00" class="style7"><? if(isset($_SESSION["sess_Username"])){echo"<a href="."admin_logout.php"."><img src="."pic/logout(red)_icon.jpeg"." width='30' alt='ออกจากระบบ' border='0'/></a>";} ?></td>
          </tr>
          <tr>
            <td colspan="2" align="center" bgcolor="#FFFFFF"><table width="98%" height="247" border="0" cellpadding="0" cellspacing="0">
                    <tr>
                        <td align="center" valign="top"><p>
 
                          </p>
                            <table width="100%" border="0" cellspacing="0" cellpadding="0">
                       <form action="<?=$_SERVER['SCRIPT_NAME'];?>" method="get" name="frmSearch" id="frmSearch" onSubmit="">
                                <tr>
                                  <td width="74%" align="center" valign="middle" bgcolor="#CCFF00">&nbsp;
                       <input name="txtkeyword" type="text" id="txtkeyword" size="50" value="<?=$_GET["txtkeyword"];?>" />
                       <input type="button" name="button2" id="button2" value="ล้าง"  onclick="txtreset()"/></td>
                                  <td width="14%" align="left" valign="middle" nowrap="nowrap" bgcolor="#CCFF00" class="">
                       <input name="cb_array[0]" type="checkbox" value="วีดีโอ" <? if($_GET["cb_array"][0] == "วีดีโอ") echo"checked"; ?> />
                                    &nbsp;<span class="style24">วีดีโอ</span><br />
                       <input name="cb_array[1]" type="checkbox" value="เสียง" <? if($_GET["cb_array"][1] == "เสียง") echo"checked"; ?> />
                                  &nbsp;<span class="style24">เสียง</span> </td>
                                  <td width="12%" align="center" valign="middle" bgcolor="#CCFF00">
                       <input type="submit" name="button" id="button" value="ค้นหา" /></td>
                                </tr>
                                <tr>
                                  <td colspan="3" align="center" valign="middle" bgcolor="#CCCC00">
    
  
  <?php 
include "connectdb.php" ;
        $SQL_SELECT_VDOTYPE = "SELECT * FROM ag_vdotype ORDER BY vdo_typeid";
        $result = mysql_query($SQL_SELECT_VDOTYPE);
		$k = 0;
		$i = 0;
        while ( $rs = mysql_fetch_array( $result ) ) //นำค่าี่ที่ผ่านการคิวรี่จากตัวแปร $result มาทำการวนลูปเพื่อเอาค่าในแต่ละฟิลด์ออกมาโชว์แสดงผล
			{ 
			$vdo_typeid = $rs[vdo_typeid];									
			$vdo_type = $rs[vdo_type];													
			
			$bgcolorj = ($j++%2)?'#f7fdea':'#ffffff';
			
		?>
<!--      <input type='checkbox' name='vdotype_array[<?=$i?>]' value="<?=$vdo_typeid?>" <? if($_GET["vdotype_array"][$i] == $vdo_typeid) echo"checked"; ?> />
				&nbsp;<font color='#372412' size='2'><?=$vdo_type?></font>&nbsp;&nbsp;-->
		<?
			 $i++;
			}
			$k = $i;
					for ($i=0;$i<$k;$i++) 
					{
					$vdotypeid = $vdotype_array[$i];
					//echo"<br>"."vdotype_array[$i] = ".$vdotype_array[$i];
					
					//echo $vdotype_a;
					//echo" "."vdotypeid = ".$vdotypeid;
					//echo"vdoid = ".$id."<br>";
					if($vdotypeid == ""){
					$vdotypeid = 0;
					
					}else{
					$vdotypeid2 .= $vdotypeid.",";
					$vdotype_[$i] = "&vdotype_array[$i]=".$vdotype_array[$i];
					}
//						if($vdotypeid != "" and $i != count($vdotype_array)){
//						$vdotypeid2 .= $vdotypeid.",";
//						}else if($i<=count($vdotype_array) and $vdotypeid != ""){
//						$vdotypeid2 .= $vdotypeid;
//						}
//					$SQL_INSERT_TYPEDETAIL = "INSERT INTO ag_vdotypedetail VALUES('$id','$vdotypeid') ;";
//					$result_vdotypedetail = mysql_db_query($dbname,$SQL_INSERT_TYPEDETAIL) or die ("ไม่สามารถ insert vdotypedetail ได้");

					}		 
					$vdotypeid2 .= "0";
	$condition_vdotype_where = "where ag_vdodetail.vdo_id IN (SELECT vdo_id from ag_vdotypedetail where vdo_typeid IN (".$vdotypeid2.") group by vdo_id )";
	$condition_vdotype_and = "and ag_vdodetail.vdo_id IN (SELECT vdo_id from ag_vdotypedetail where vdo_typeid IN (".$vdotypeid2.") group by vdo_id )";
					if(count($vdotype_array) == 0){
					$condition_vdotype_where = "";
					$condition_vdotype_and = "";
					}
					// echo "<br>".$vdotypeid2;
      
 ?>
 
 
 
 <?php 
							//=======================================================================================//
	$keyword = $_GET["txtkeyword"];
	if($keyword != ''){
		$doc = new DOMDocument();
		$url='xml/KitchenGardenVegetables.xml';
		$doc->load($url);
		
		$items = $doc->getElementsByTagName($keyword)->item(0);
		if($items->nodeType == XML_ELEMENT_NODE){
				$count_syn_of =0;  
				$Target_syn_of = '';
			
				$count_is_a =0;  
				$Target_is_a = '';
			  
				$i=0;
				$Target = '';
				
				$test = $items->childNodes;
				
						foreach($test as $testloop){
						
								if($testloop->nodeType == XML_ELEMENT_NODE) {
							
			//							echo '<br>';
			//							echo 'nodeType ->'.$testloop->nodeType.'<br>';
			//							echo 'nodeName -> '.$testloop->nodeName.'<br>';
			//							echo 'nodeAttType -> '.$testloop->attributes->item(0)->nodeValue.'<br>';
			//							echo 'nodeValue -> '.$testloop->nodeValue.'<br>';
										if ($testloop->attributes->item(0)->nodeValue == ''){
										/*echo 'I Like This Node'.'<br>';*/
										$Target[$i] = $testloop->nodeName;
										/*echo $Target[$i].'<br>';*/
										$i++;
										}
										
										if ($testloop->attributes->item(0)->nodeValue == 'is-a'){
										$Target_is_a[$count_is_a] = $testloop->nodeValue;
										$count_is_a++;
										}
										
										if ($testloop->attributes->item(0)->nodeValue == 'syn-of'){
										$Target_syn_of[$count_syn_of] = $testloop->nodeValue;
										$count_syn_of++;
										}
							
								}
						
					  }
					  
						for($a=0;$a<$i;$a++){
						//echo 'Target['.$a.'] = ',$Target[$a],'<br>';
						}
						//echo '<br>';
					  //echo 'is-a = '.$count_is_a;
					  //echo '<br>';
					  
						for($i=0;$i<$count_is_a;$i++){
						//echo 'Target_is_a['.$i.'] = ',$Target_is_a[$i],'<br>';
						$keyword_is_a[$i] = $Target_is_a[$i];
						}
					 // echo 'syn-of = '.$count_syn_of;
					 // echo '<br>';
					  
						for($i=0;$i<$count_syn_of;$i++){
						//echo 'Target_syn_of['.$i.'] = ',$Target_syn_of[$i],'<br>';
						$keyword_syn_of[$i] = $Target_syn_of[$i];
						
							if(($count_syn_of - $i)!=1){ //========= ต่อ string sql command SYN-OF =========//
							$condition_syn_of = $condition_syn_of."vdo_name LIKE '%".$Target_syn_of[$i]."%' OR ";
							}else $condition_syn_of = $condition_syn_of."vdo_name LIKE '%".$Target_syn_of[$i]."%' ";
						}
				
			}
	}
	
	for($i=0;$i<$count_is_a;$i++){
	
		if(($count_is_a - $i)!=1){
		$condition_is_a1 = $condition_is_a1."vdo_name LIKE '%".$Target_is_a[$i]."%' OR ";
		}else $condition_is_a1 = $condition_is_a1."vdo_name LIKE '%".$Target_is_a[$i]."%' ";
			
	}
	
	for($j=0;$j<$count_syn_of;$j++){
			
		if(($count_syn_of - $j)!=1){ 
		$condition_is_a2 = $condition_is_a2."vdo_name NOT LIKE '%".$Target_syn_of[$j]."%' AND ";
		}else $condition_is_a2 = $condition_is_a2."vdo_name NOT LIKE '%".$Target_syn_of[$j]."%' AND vdo_name NOT LIKE '%".$_GET["txtkeyword"]."%' ";
			
	}
	
	$count_cbarray = count($_GET["cb_array"]);
	if($count_cbarray=='2'){
					$condition2 = "".$condition_vdotype_and;
	}
	else if($count_cbarray=='1'){
					
						for ($i=0;$i<$count_cbarray;$i++) {
						$cb_array = $cb_array[$i];
						}
						
						if($_GET["cb_array"][0]=="วีดีโอ"){
						$condition2 = $condition2." AND media_typeid = '1' ".$condition_vdotype_and;
						}else if($_GET["cb_array"][1]=="เสียง"){
						$condition2 = $condition2." AND media_typeid = '2' ".$condition_vdotype_and;
						}
						
	}else if($count_cbarray=='0'){
			$condition2 = $condition2.$condition_vdotype_and;
	}
	
	if($condition_is_a2 == ""){ $condition_is_a2 = $condition_is_a1;}
	//echo '<br> condition -> '.'('.$condition_syn_of.')'.'<br>';
	
	$SQL_SELECT[0] = "SELECT * FROM ag_vdodetail WHERE (vdo_name LIKE '%".$_GET["txtkeyword"]."%') ".$condition2." order by vdo_id ";
	//echo '<br>'.$SQL_SELECT[0].'<br>';
	
	$SQL_SELECT[1] = "SELECT * FROM ag_vdodetail WHERE ".'('.$condition_syn_of.') '.$condition2.' order by vdo_id';
	//echo '<br>'.$SQL_SELECT[1].'<br>';
	
	$SQL_SELECT[2] = "SELECT * FROM ag_vdodetail WHERE ".'('.$condition_is_a1.') AND ('.$condition_is_a2.') '.$condition2.' order by vdo_id ';
	//echo '<br>'.$SQL_SELECT[2].'<br>';
//=======================================================================================//
?> 								</td>
                                </tr>
                              </form>
                            </table>
                  <? 
include "connectdb.php" ;
$tb="ag_vdodetail"; 
$count_cbarray = count($_GET["cb_array"]);

////ถ้าไม่เป็นค่าว่าง////
if($_GET["txtkeyword"] != "") 
{
		/////////////////////////////////////// เก็บสถิติคำค้น (word) //////////////////////////////////////////
		if(isset($_SESSION["sess_userid"]) and !isset($_SESSION["sess_Username"])){
			$txtkeyword = $_GET["txtkeyword"];
			$user_id = $_SESSION["sess_userid"];
			
			$SQL_SELECT_WORD="SELECT * FROM ag_word WHERE word='$txtkeyword' ;";
			$db_query=mysql_db_query($dbname,$SQL_SELECT_WORD);
			$num_rows=mysql_num_rows($db_query);
			
			$word_id = $db_query[word_id];
			
			if($num_rows == 0){ //ถ้าไม่ซ้ำก็ insert
				$one = 1;
				$SQL_SELECT_MAXWORD = "SELECT MAX(word_id) FROM ag_word ;";
				$resultmax = mysql_query($SQL_SELECT_MAXWORD);
				$resultmax2 = mysql_result($resultmax,0);
				$resultmax3 = $resultmax2+1;
	
				//echo"$resultmax2<br>";
				//echo"$resultmax3<br>";
	
				$SQL_INSERT_WORD = "INSERT INTO ag_word VALUES('$resultmax3','$txtkeyword','1') ;";
				$result = mysql_db_query($dbname,$SQL_INSERT_WORD)or die("ไม่สามารถเพิ่มคำค้นได้ (word)");
				
				$SQL_INSERT_SEARCH = "INSERT INTO ag_search VALUES('$user_id','$resultmax3') ;";
				$result = mysql_db_query($dbname,$SQL_INSERT_SEARCH)or die("ไม่สามารถเพิ่มสถิติการค้นได้ได้ (search)");			
				
			}else if($num_rows == 1){ //ถ้ามีคำซ้ำก็ update
				
				$SQL_UPDATE_WORD = "UPDATE ag_word SET word_count=word_count+1 WHERE word='$txtkeyword' ;";
				$result = mysql_db_query($dbname,$SQL_UPDATE_WORD)or die("ไม่สามารถ update คำค้นได้ (word)");
				
				$SQL_SELECT_WORDID = "SELECT word_id FROM ag_word WHERE word='$txtkeyword' ;";
				$db_query=mysql_query($SQL_SELECT_WORDID);
				$word_id = mysql_result($db_query,0);
				
				$SQL_INSERT_SEARCH = "INSERT INTO ag_search VALUES('$user_id','$word_id') ;";
				$result = mysql_db_query($dbname,$SQL_INSERT_SEARCH)or die("ไม่สามารถเพิ่มสถิติการค้นได้ได้ (search)");	
			}
		
		}
		////////////////////////////////////////////////////////////////////////////////////////////////
//		if($_GET["txtfield"]=="1"){
		$condition = " WHERE (vdo_name LIKE '%".$_GET["txtkeyword"]."%' OR vdo_desc LIKE '%".$_GET["txtkeyword"]."%') ";
		
				if($count_cbarray=='2'){
					$condition = $condition.$condition_vdotype_and;
				}
				else if($count_cbarray=='1'){
					
						for ($i=0;$i<$count_cbarray;$i++) {
						$cb_array = $cb_array[$i];
				}
						
							if($_GET["cb_array"][0]=="วีดีโอ"){
							$condition = $condition." AND media_typeid = '1' ".$condition_vdotype_and;
							}else if($_GET["cb_array"][1]=="เสียง"){
							$condition = $condition." AND media_typeid = '2' ".$condition_vdotype_and;
							}
						
				}else if($count_cbarray=='0'){
					$condition = $condition.$condition_vdotype_and;
				}
		
//		}
//		else if($_GET["txtfield"]=="2"){
//		$condition = "WHERE (vdo_name LIKE '%".$_GET["txtkeyword"]."%' OR vdo_keyword LIKE '%".$_GET["txtkeyword"]."%') ";
//		//$condition = " WHERE (vdo_name LIKE '%".$_POST["txtkeyword"]."%') ";
//		
//	
//			if($count_cbarray=='2'){
//			$condition = $condition;
//			}
//			else if($count_cbarray=='1'){
//			
//				for ($i=0;$i<$count_cbarray;$i++) {
//				$cb_array = $cb_array[$i];
//				}
//				
//					if($_GET["cb_array"][0]=="วีดีโอ"){
//					$condition = $condition." AND media_typeid = '1'";
//					}else if($_GET["cb_array"][1]=="เสียง"){
//					$condition = $condition." AND media_typeid = '2'";
//					}
//				
//			}
//		}		
		
}
	////ถ้าเป็นค่าว่าง////
else if ($_GET["txtkeyword"] == "")
{
		if($count_cbarray=='2'){
		$condition = "".$condition_vdotype_where;
		}else if($count_cbarray=='1'){
		
			for ($i=0;$i<$count_cbarray;$i++) {
			$cb_array = $cb_array[$i];
			}
			
				if($_GET["cb_array"][0]=="วีดีโอ"){
				$condition = "WHERE media_typeid = '1' ".$condition_vdotype_and;
				}else if($_GET["cb_array"][1]=="เสียง"){
				$condition = "WHERE media_typeid = '2' ".$condition_vdotype_and;
				}
			
		}else if($count_cbarray=='0'){
			$condition = $condition.$condition_vdotype_where;
		}
	
}
//$loop = 0;

for($loop=0;$loop<3;$loop++){
mysql_select_db($dbname) or die("เลือกฐานข้อมูลไม่ได้"); /* ทําการเลือกฐานขอมูลกอน */ 
if($keyword != ''){
$sql = $SQL_SELECT[0];
}else{
$sql="SELECT * FROM $tb $condition ORDER BY vdo_id";
$sql="";
}

if($loop==1){$sql = $SQL_SELECT[1];
}else if($loop==2){$sql = $SQL_SELECT[2];
}
//echo $sql;
$db_query = "";
	$db_query[$loop]=mysql_db_query($dbname,$sql); 
	$db_q = $db_query[$loop];
	if (!$db_q){
	continue;
	}
	
	$num_rows=mysql_num_rows($db_q); /* นับ Reccord ทีพบ */

	$numrow[$loop] = mysql_num_rows($db_q);
	if($loop==0 && $num_rows != 0){echo "จำนวน $num_rows รายการ";}
	else if($loop==1 && $num_rows != 0){echo "<div align='left' class='style8'> คำเหมือน : จำนวน $num_rows รายการ</div>";}
	else if($loop==2 && $num_rows != 0){echo "<div align='left' class='style8'> คำใกล้เคียง : จำนวน $num_rows รายการ</div>";}

/*$Per_Page = 10;   // จำนวนรายการสื่อมัลติมีเดียต่อ 1 หน้า
 
    $Page = $_GET["Page"];
    if(!$_GET["Page"])
    {
        $Page=1;
    }
 
    $Prev_Page = $Page-1;
    $Next_Page = $Page+1;
 
    $Page_Start = (($Per_Page*$Page)-$Per_Page);
    if($num_rows<=$Per_Page)
    {
        $Num_Pages =1;
    }
    else if(($num_rows % $Per_Page)==0)
    {
        $Num_Pages =($num_rows/$Per_Page) ;
    }
    else
    {
        $Num_Pages =($num_rows/$Per_Page)+1;
        $Num_Pages = (int)$Num_Pages;
    }
 
 
    $sql .=" ASC LIMIT $Page_Start , $Per_Page";
	$db_query[$loop]=mysql_db_query($dbname,$sql); 
	$db_q = $db_query[$loop];*/
$numrow_sum = $numrow[0]+$numrow[1]+$numrow[2];
//$numrow_sum=='0' and $loop=='2'
if($numrow_sum=='0' and $loop==2){
	echo"<left><br><div align='center' class='style8'>ไม่พบข้อมูลที่ต้องการ</div></left>";
	//break;
}else{
			if($loop==0){
						//echo"<left><br>จํานวน $num_rows รายการ</left>"; 

                          echo"<table width='100%' height='45' border='1' align='center' cellpadding='0' cellspacing='0' bordercolor='#999999' bgcolor='#CCFF00'>";
                          echo"<tr>";
                          echo"<td width='50' class='style22'><div align='center' class='style24'>หมวด</div></td>";
                          echo"<td width='' class='style22'><div align='center' class='style24'>รายการ</div></td>";
                          echo"<td width='50' class='style22'><div align='center' class='style24'>จำนวน<br />ผู้ใช้บริการ</div></td>";
                          echo"<td width='87' class='style22'><div align='center' class='style24'>ระดับความสนใจ</div></td>";
                          echo"<td width='50' class='style22'><div align='center' class='style24'>จำนวน<br />ผู้โหวต</div></td>";
                          echo"</tr>";
                          echo"</table>";
						  
			}
 }                         
?>
                            
                            <?  ///////////////// วน loop แสดงข้อมูล //////////////////////

$a=0; 
$colorzero="#FFF2A4";
$colorone="#FEE86E";
$bgcolor="";
$vdo_icon = "wmv_icon.gif";
$sound_icon = "wma_icon.jpg";
$unknow_icon = "unknow_icon.jpg";
//while($a < $num_rows) 

while($result = mysql_fetch_array($db_query[$loop])) 
{ 
//$result = mysql_fetch_array($db_query); 
$vdo_id=$result[vdo_id]; 
$vdo_name=$result[vdo_name]; 
$vdo_desc=$result[vdo_desc]; 
$vdo_view=$result[vdo_view]; 
$vdo_dateadd=$result[vdo_dateadd]; 
$mediatype=$result[media_typeid];

if($mediatype==1){
	$vdo_pic=$vdo_icon;
} else if($mediatype==2){
	$vdo_pic=$sound_icon;
}else $vdo_pic=$unknow_icon;
//$vdo_view=$result[vdo_name]; 
$_SESSION["sess_Url"]=$result[vdo_url]; 

//$SQL_SELECT_RATING="SELECT * FROM ag_vdodetail where vdo_id = $vdo_id";
//$db_query_rating=mysql_db_query($dbname,$SQL_SELECT_RATING); 
//$result_rating = mysql_fetch_array($db_query_rating); 
$votesum=$result[vdo_votesum]; 
$votecount=$result[vdo_votecount]; 

if($votesum==0 and $votecount==0){
$rating=0;
$votecount=0;
} else $rating = $votesum/$votecount;
////////// ปัดเศษทศนิยม ถ้าน้อยกว่า 0.5 ให้ปัดลง 0.5-0.75 ให้ปัดลงเศษ 0.5 ถ้า 0.75-1 ให้ปัดขึ้น //////////
		if(($rating-(int)$rating)>=0.5 and ($rating-(int)$rating)<0.75)
		{$rating++;
		$rating=(int)$rating-0.5;
		}
		else if(($rating-(int)$rating)>=0.75 and ($rating-(int)$rating)<1)
		{
		$rating++;
		$rating=(int)$rating;
		}
		else $rating=(int)$rating;
//////////////////////////////จบการปัดเศษ//////////////////////////////
	if (($a % 2) == 0) {
	$bgcolor=$colorzero;
	}
	else $bgcolor=$colorzero;

//========== vdo ใหม่จะไม่เกิน 4 อาทิตย์ นับจากวันที่เพิ่มข้อมูลลงไป ===========//
$today = Date("Y/m/d");
$str = $vdo_dateadd;  
list($year,$month,$day) = explode("/",$str);  
$mydate = (int)($year.$month.$day);
//echo "$mydate";  

//echo'<br>';

$str = $today;  
list($year2,$month2,$day2) = explode("/",$str); 
$today =  (int)($year2.$month2.$day2);
//echo "$today";  
//================================================================//
	
	?>
                          
                          <table width="100%" border="1" align="center" cellpadding="0" cellspacing="0" bordercolor="#999999">
                              <tr>
                                <td width="50" bgcolor="<?=$bgcolor?>" class="style23"><!--<div align="center" class="style7"> <?echo"$vdo_id";?></div></td>    -->
                                    <div align="center" class="style7"> <img src='pic/<?=$vdo_pic?>' width='35'  border='0' /></div></td>
                                <td width="" height="70" align="left" bgcolor="<?=$bgcolor?>" class="style19 style19"><br />
                                    <div align="left" class="style24"> &nbsp;&nbsp;<a href="vdo_player.php?id=<?=$vdo_id?>" target="_self"><?echo"$vdo_name";?> </a>
                                    <?php if($today-$mydate <= 30) echo "<img src='pic/new.gif' />"; ?></div>
                                  <div align="left" class="style26"> &nbsp;&nbsp;&nbsp;&nbsp;<?echo"$vdo_desc";?></div>
                                  <br />                                </td>
                                <td width="50" bgcolor="<?=$bgcolor?>" class="style19 style19"><div align="center" class="style24"><?echo"$vdo_view";?></div></td>
                                <td width="87" bgcolor="#CCCCCC" class="style19 style19"><div align="center" class="style24"><img src="pic/star_<?=$rating?>.gif" height="15" /></div></td>
                                <td width="50" bgcolor="<?=$bgcolor?>" class="style19 style19"><div align="center" class="style24"><?echo"$votecount";?></div></td>
                              </tr>
                            </table>
                          
                            <? 
    $a++; 
} 
if($keyword==''){$loop=3;}

}///////จบ for loop
echo"<br>";
//echo"<center><br>จํานวน Reccord = $num_rows</center>"; 
//mysql_close(); 
?>
                            <!--Total <?php /*?><?= $num_rows;?><?php */?> Record :-->
                            <?php /*?><?=$Num_Pages;?><?php */?>
                            <!--Page :-->
                            <?
 	/*$cbarray0 = '&cb_array[0]=';
 	if($_GET["cb_array"][0] == "วีดีโอ") {
	$cbarray0 = '&cb_array[0]='.$_GET["cb_array"][0];
	}else $cbarray0 = '';
	
	$cbarray1 = '&cb_array[1]=';
	if($_GET["cb_array"][1] == "เสียง"){
	$cbarray1 = '&cb_array[1]='.$_GET["cb_array"][1];
	}else $cbarray1 = '';
		
	for($i=0;$i<$k;$i++){
	$vdotype .= $vdotype_[$i];
	}			  
				  
				  
    if($Prev_Page)
    {
        echo " <b><a href='$_SERVER[SCRIPT_NAME]?Page=$Prev_Page
		&txtfield=$_GET[txtfield]
		&txtkeyword=$_GET[txtkeyword]
		$cbarray1
		$cbarray0
		$vdotype
		' ><font color = #006600><< ก่อนหน้า</font></a></b> "; //&cb_array[]=ภาพและเสียง&cb_array[]=เสียง&button=Search
    }
 
    for($i=1; $i<=$Num_Pages; $i++){
        if($i != $Page )
        {
            echo " <a href='$_SERVER[SCRIPT_NAME]?Page=$i
			&txtfield=$_GET[txtfield]
			&txtkeyword=$_GET[txtkeyword]
			$cbarray1
			$cbarray0
			$vdotype
			'><font color = #006600>$i</font></a> ";
        }
        else
        {
           if($num_rows != '0') echo "<b><font color = black>[ $i ]</font></b>";
        }
    }
    if($Page!=$Num_Pages)
    {
        echo " <b><a href ='$_SERVER[SCRIPT_NAME]?Page=$Next_Page
		&txtfield=$_GET[txtfield]
		&txtkeyword=$_GET[txtkeyword]
		$cbarray1
		$cbarray0
		$vdotype
		'><font color = '#006600'>ถัดไป>></font></a></b> ";
    }*/
     
    mysql_close();
 
     
    ?>                         </td>
                      </tr>
                    </table>
                    <p>&nbsp;</p></td>
          </tr>
          <tr>
            <td height="25" colspan="2" bgcolor="#CCFF00">&nbsp;</td>
          </tr>
        </table></td>
        <td width="25" align="right" background="img/tb/tb_right.jpg">&nbsp;</td>
      </tr>
      <tr>
        <td width="25" height="25" background="img/tb/tb_left_foot.jpg">&nbsp;</td>
        <td height="25" background="img/tb/tb_foot.jpg">&nbsp;</td>
        <td width="25" height="25" background="img/tb/tb_right_foot.jpg">&nbsp;</td>
      </tr>
    </table></td>
  </tr>
  <tr>
    <td width="1024" align="center" valign="bottom"><table width="50%" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td height="25">&nbsp;</td>
      </tr>
    </table>
        <table width="1024" border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td height="50"><table width="100%" border="0" cellspacing="0" cellpadding="0">
                <tr>
                  <td width="25" height="25" background="img/tb/tb_left_top.jpg">&nbsp;</td>
                  <td background="img/tb/tb_top.jpg">&nbsp;</td>
                  <td width="25" height="25" background="img/tb/tb_right_top.jpg">&nbsp;</td>
                </tr>
                <tr>
                  <td width="25" align="left" background="img/tb/tb_left.jpg">&nbsp;</td>
                  <td height="25" align="center" valign="middle" bgcolor="#CCFF00"><table width="96%" border="0" cellspacing="0" cellpadding="0">
                      <tr>
                        <td height="25" align="center"><strong>ศูนย์ความรู้ด้านการเกษตร สำนักหอสมุด มหาวิทยาลัยเกษตรศาสตร์</strong></td>
                      </tr>
                  </table></td>
                  <td width="25" align="right" background="img/tb/tb_right.jpg">&nbsp;</td>
                </tr>
                <tr>
                  <td width="25" height="25" background="img/tb/tb_left_foot.jpg">&nbsp;</td>
                  <td background="img/tb/tb_foot.jpg">&nbsp;</td>
                  <td width="25" height="25" background="img/tb/tb_right_foot.jpg">&nbsp;</td>
                </tr>
            </table></td>
          </tr>
      </table></td>
  </tr>
</table>
<p>&nbsp;</p>
</body>
</html>
<?php 
}
?>